package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {



    @Reference
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> roles =roleService.findAll();
        return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,roles);

    }


    //添加
    @PostMapping("/add")
    public Result add(@RequestBody Role role , Integer[] permissionIds,Integer[] menuids) {
        log.info("[角色-添加]data:{},permissionIds:{},menuids:{}",role,permissionIds,menuids);
       roleService.add(role,permissionIds,menuids);
        return new Result(true,MessageConst.ADD_ROLE_SUCCESS);
    }

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        //调用DAO层查询并返回
        PageResult pageResult = roleService.findPage(queryPageBean);
        //构造返回结果并返回
        return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,pageResult);
    }

    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("[角色-编辑]id:{}",id);
        Role role=roleService.findById(id);
        return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,role);

    }


    @PostMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuids){
        log.info("[角色-编辑]role:{},permissionIds:{},menuids{}",role,permissionIds,menuids);
        roleService.edit(role,permissionIds,menuids);
        return new Result(true,MessageConst.EDIT_ROLE_SUCCESS);


    }


    @PostMapping("/delete")
    public Result delById( Integer id){
        log.info("[删除-角色]id:{}",id);

        roleService.delById(id);
        return new Result(true,MessageConst.DELETE_ROLE_SUCCESS);
    }


}
